package com.xxx.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * 添加水印工具类
 */
public class WaterMarkUtils {

	/**
	 * 给图片添加文字水印
	 * 
	 * @param srcImgPath
	 *            需要添加水印的图片的路径
	 * @param outImgPath
	 *            添加水印后图片输出路径
	 * @param markContentColor
	 *            水印文字的颜色
	 * @param waterMarkContent
	 *            水印的文字
	 * @throws IOException
	 */
	public void markImgByWord(String srcImgPath, String outImgPath, Color markContentColor, String waterMarkContent)
			throws IOException {
		// 读取原图片信息
		File srcImgFile = new File(srcImgPath);
		Image srcImg = ImageIO.read(srcImgFile);
		int srcImgWidth = srcImg.getWidth(null);
		int srcImgHeight = srcImg.getHeight(null);
		// 加水印
		BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bufImg.createGraphics();
		g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
		// Font font = new Font("Courier New", Font.PLAIN, 12);
		Font font = new Font("宋体", Font.PLAIN, 20);
		g.setColor(markContentColor); // 根据图片的背景设置水印颜色

		g.setFont(font);
		// 水印加载图片右下角
		// int x = srcImgWidth - getWatermarkLength(waterMarkContent, g) - 3;
		// int y = srcImgHeight - 3;
		// 水印加载图片中央
		int x = (srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2;
		int y = srcImgHeight / 2;
		g.drawString(waterMarkContent, x, y);
		g.dispose();
		// 输出图片
		FileOutputStream outImgStream = new FileOutputStream(outImgPath);
		ImageIO.write(bufImg, "jpg", outImgStream);
		outImgStream.flush();
		outImgStream.close();
	}

	/**
	 * 获取水印文字总长度
	 * 
	 * @param waterMarkContent
	 *            水印的文字
	 * @param g
	 * @return 水印文字总长度
	 */
	private int getWatermarkLength(String waterMarkContent, Graphics2D g) {
		return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
	}

	/**
	 * 给图片添加图片水印
	 * 
	 * @param iconPath
	 *            水印图片路径
	 * @param srcImgPath
	 *            源图片路径
	 * @param targerPath
	 *            目标图片路径
	 * @throws IOException
	 */
	public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) throws IOException {
		markImageByIcon(iconPath, srcImgPath, targerPath, null);
	}

	/**
	 * 给图片添加水印、可设置水印图片旋转角度
	 * 
	 * @param iconPath
	 *            水印图片路径
	 * @param srcImgPath
	 *            源图片路径
	 * @param targerPath
	 *            目标图片路径
	 * @param degree
	 *            水印图片旋转角度
	 * @throws IOException
	 */
	public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree)
			throws IOException {
		Image srcImg = ImageIO.read(new File(srcImgPath));

		BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null),
				BufferedImage.TYPE_INT_RGB);

		// 得到画笔对象
		// Graphics g= buffImg.getGraphics();
		Graphics2D g = buffImg.createGraphics();

		// 设置对线段的锯齿状边缘处理
		g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
				null);
		if (null != degree) {
			// 设置水印旋转
			g.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
		}

		// 水印图象的路径 水印一般为gif或者png的，这样可设置透明度
		ImageIcon imgIcon = new ImageIcon(iconPath);

		// 得到Image对象。
		Image img = imgIcon.getImage();

		float alpha = 0.5f; // 透明度
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
		// 表示水印图片的位置
		g.drawImage(img, 150, 300, null);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
		g.dispose();

		OutputStream os = new FileOutputStream(targerPath);

		// 生成图片
		ImageIO.write(buffImg, "JPG", os);
		os.close();
	}

	/**
	 * 测试
	 * 
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// 给图片添加文字水印
		// String srcImgPath = "C:/Users/xxx/Desktop/3.jpg";
		// String outImgPath = "C:/Users/xxx/Desktop/33.jpg";
		// String markContent = "图片来源：58同城房产经纪大学";
		// new WaterMarkUtils().markImgByWord(srcImgPath, outImgPath, Color.red,
		// markContent);

		// 给图片添加图片水印
		String iconPath = "C:/Users/xxx/Desktop/head.jpg";
		String srcImgPath = "C:/Users/xxx/Desktop/3.jpg";
		String targerPath = "C:/Users/xxx/Desktop/3a.jpg";
		String targerPath2 = "C:/Users/xxx/Desktop/3b.jpg";
		// 给图片添加水印
		WaterMarkUtils.markImageByIcon(iconPath, srcImgPath, targerPath);
		// 给图片添加水印,水印旋转-45
		WaterMarkUtils.markImageByIcon(iconPath, srcImgPath, targerPath2, -45);

	}
}
